package com.ciwong.epaper.modules.me.ui;

import android.view.View;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.mobilelib.bean.SchoolDetail;
import com.ciwong.epaper.modules.epaper.dao.EpaperDao;
import com.ciwong.epaper.modules.me.bean.KefuBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.ui.BaseActivity;

import java.util.List;

/**
 * 联系客服
 * Created by Administrator on 2016/9/13.
 */
public class KeFuActivity extends BaseActivity implements View.OnClickListener {

    private EApplication application;
    private int mSchoolId;

    private TextView tx_gw_name, tx_gw_phone;

    @Override
    protected int setView() {
        return R.layout.activity_ke_fu;
    }

    @Override
    protected void findViews() {
        tx_gw_name = findViewById(R.id.tx_gw_name);
        tx_gw_phone = findViewById(R.id.tx_gw_phone);
    }

    @Override
    protected void initEvent() {
        findViewById(R.id.my_info_account_layout).setOnClickListener(this);
        findViewById(R.id.my_info_name_layout).setOnClickListener(this);
        setGoBackListener(new GoBackListener() {
            @Override
            public void goBack() {
                onBackPressed();
            }
        });
    }

    @Override
    protected void init() {
        setTitleText(getString(R.string.contact_us));
        application = (EApplication) getApplication();
    }

    @Override
    protected void loadData() {

        getServiceBook();
    }


    @Override
    public void onBackPressed() {
        finish();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.my_info_account_layout) {
            //拨打客服电话
            MeJumpManager.jumpToCallPhone(0, KeFuActivity.this, getString(R.string.service_phone));
        }else if (id == R.id.my_info_name_layout){
            if (null != tx_gw_phone.getText() && !"".equals(tx_gw_phone.getText())){
                MeJumpManager.jumpToCallPhone(0, KeFuActivity.this, tx_gw_phone.getText().toString());
            }
        }
    }

    private void getServiceBook() {
        SchoolDetail schoolDetail = application.getSchoolDetail();
        final int schoolId = schoolDetail == null ? 0
                : schoolDetail.getSchoolId();
        mSchoolId = schoolId;
        getAdvisor(schoolId);
    }

    /**
     * 获取教育顾问
     *
     * @param schoolId 学校ID
     */
    private void getAdvisor(final int schoolId) {
        showMiddleProgressBar(getTitleText());
        EpaperDao.getInstance().getAdvisor(schoolId, EApplication.BRAND_ID, new BaseExtCallBack(KeFuActivity.this, EApplication.getInstance().getUserInfoBase().getUserId() + "") {
            @Override
            public void failed(int errorCode, Object data) {
                super.failed(errorCode, data);
                hideMiddleProgressBar();
            }

            @Override
            public void failed(Object data) {
                super.failed(data);
                hideMiddleProgressBar();
            }

            @Override
            public void success(Object data) {
                try {
                    hideMiddleProgressBar();
                    List<KefuBean> kefuBeanList = (List<KefuBean>) data;
                    if (!kefuBeanList.isEmpty()) {
                        KefuBean kefuBean = kefuBeanList.get(0);
                        setDefuatWechatImg(kefuBean);
                    }
                } catch (Exception e) {
                    e.getStackTrace();
                }


            }
        });
    }


    /**
     * 设置微信图片
     */
    private void setDefuatWechatImg(KefuBean kefuBean) {
        if (kefuBean.getUserName() != null && kefuBean.getUserName() != "") {
            tx_gw_name.setText(kefuBean.getUserName());
        } else {
            tx_gw_name.setText("");
        }
        if (kefuBean.getMobile() != null) {
            tx_gw_phone.setText(kefuBean.getMobile());
        }
    }
}
